fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419
fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419nileshpatil6 wants to merge 2 commits intogoogle:mainfrom
Conversation
|
I have read the Contributor License Agreement and I hereby agree to its terms. |
|
Response from ADK Triaging Agent Hello @nileshpatil6, thank you for creating this PR! Could you please include a |
Testing PlanManual verification: import json
from google.adk.telemetry.tracing import _safe_json_serialize
# Test 1: Circular reference (previously raised ValueError)
circular = {}
circular['self'] = circular
assert _safe_json_serialize(circular) == '<not serializable>'
# Test 2: Deeply nested structure (previously raised RecursionError)
deep = {}
node = deep
for _ in range(1000):
node['child'] = {}
node = node['child']
result = _safe_json_serialize(deep)
assert result == '<not serializable>'
# Test 3: Normal objects still serialize correctly
assert _safe_json_serialize({'key': 'value'}) == '{"key": "value"}'
print('All tests passed')Before fix: After fix: Returns |
|
Hi @nileshpatil6 , Thank you for your contribution through this pull request! This PR has merge conflicts that require changes from your end. Could you please rebase your branch with the latest main branch to address these? Once this is complete, please let us know so we can proceed with the review. |
Upstream already added ValueError; add RecursionError to guard against deeply nested structures exceeding Python's recursion limit when json.dumps is called inside telemetry finally blocks. Fixes google#5411
|
Rebased on latest main. I noticed upstream already included |
Problem
_safe_json_serializeintelemetry/tracing.pyonly catchesTypeErrorandOverflowError, butjson.dumpscan also raise:ValueError— e.g. circular references (json.dumpsraisesValueError: Circular reference detected)RecursionError— e.g. deeply nested structures exceeding Python's recursion limitBecause this function is called inside
finallyblocks during tool execution telemetry, an uncaught exception here can interrupt normal execution flow and affect tool results.Fixes #5411
Fix
Add
ValueErrorandRecursionErrorto the existing except clause: